{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# Definition for singly-linked list.\n",
    "class ListNode:\n",
    " def __init__(self, x):\n",
    "     self.val = x\n",
    "     self.next = None\n",
    "\n",
    "class Solution:\n",
    "    def partition(self, head: ListNode, x: int) -> ListNode:\n",
    "        head_big = ListNode(None)\n",
    "        head_big.next = None\n",
    "        head_small = ListNode(None)\n",
    "        head_small.next = None\n",
    "        tail_big = head_big\n",
    "        tail_small = head_small\n",
    "        p = head\n",
    "        while(p):\n",
    "            temp = p.next\n",
    "            if(p.val < x):\n",
    "                tail_small.next = p\n",
    "                tail_small = p\n",
    "                tail_small.next = None\n",
    "            else:\n",
    "                tail_big.next = p\n",
    "                tail_big = p\n",
    "                tail_big.next = None\n",
    "            p = temp\n",
    "        tail_small.next = head_big.next\n",
    "        return head_small.next\n",
    "        \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
